/*
 * These files are distributed under the terms of the GNU GPLv2, the GNU General
 * Public version 2. See LICENSE.txt
 */
package fileCommodore.plugin.pathHopper;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

import org.apache.commons.vfs.*;

/**
 * @author nado18@gmail.com, on Jul 15, 2007
 */
public class PathHopper extends JPanel
{
	private JPanel pane = new JPanel();
	private JScrollPane sp = new JScrollPane( pane );
	private JLabel scrollLeft = new JLabel( "\u2190" );
	private JLabel scrollRight = new JLabel( "\u2192" );
	private Vector<MouseListener> listeners = new Vector<MouseListener>();
	private FileObject selected = null;
	private Font font;
	private HashMap<PathHopperPart, FileObject> buttonPaths =
			new HashMap<PathHopperPart, FileObject>();
	
	public PathHopper(int fontSize)
	{
		font = new Font( "Arial", Font.PLAIN, 9 * fontSize / 10 );
		scrollLeft.setBorder( null );
		scrollRight.setBorder( null );
		sp.setBorder( null );
		
		// Add a button right away otherwise the JScrollPane doesn't get enough
		// pixels allocated
		PathHopperPart b = new PathHopperPart( "File Commodore" );
		b.setFont( font );
		pane.add( b );
		
		setLayout( new BorderLayout() );
		add( scrollLeft, BorderLayout.WEST );
		add( sp, BorderLayout.CENTER );
		add( scrollRight, BorderLayout.EAST );
		
		sp.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER );
		sp.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
		
		scrollLeft.addMouseListener( new MouseAdapter()
		{
			@Override
			public void mouseClicked( MouseEvent e )
			{
				JViewport vp = sp.getViewport();
				Point p = vp.getViewPosition();
				Rectangle d = vp.getVisibleRect();
				
				int x = p.x - 3 * d.width / 4;
				if (x < 0)
				{
					x = 0;
				}
				p.x = x;
				
				vp.setViewPosition( p );
			}
		} );
		
		scrollRight.addMouseListener( new MouseAdapter()
		{
			@Override
			public void mouseClicked( MouseEvent e )
			{
				JViewport vp = sp.getViewport();
				Point p = vp.getViewPosition();
				Rectangle d = vp.getViewRect();
				
				int x = p.x + 3 * d.width / 4;
				if (x > pane.getWidth() - d.width)
				{
					x = pane.getWidth() - d.width;
				}
				p.x = x;
				
				vp.setViewPosition( p );
			}
		} );
	}
	
	public void addMouseListener( MouseListener listener )
	{
		listeners.add( listener );
	}
	
	public void removeMouseListener( MouseListener listener )
	{
		listeners.remove( listener );
	}
	
	private synchronized void notifyListeners( MouseEvent ev )
	{
		for (MouseListener listener : listeners)
		{
			listener.mouseClicked( ev );
		}
	}
	
	public synchronized void setPath( FileObject fo )
	{
		selected = null;
		buttonPaths.clear();
		pane.removeAll();
		
		while (null != fo)
		{
			String baseName = fo.getName().getBaseName();
			if (baseName.equals( "" ))
			{
				baseName = "/";
			}
			
			PathHopperPart b = new PathHopperPart( baseName );
			b.setFont( font );
			pane.add( b, 0 );
			
			buttonPaths.put( b, fo );
			
			b.addMouseListener( new MouseAdapter()
			{
				@Override
				public void mouseClicked( MouseEvent e )
				{
					selected = buttonPaths.get( e.getSource() );
					notifyListeners( e );
					super.mouseClicked( e );
				}
			} );
			
			try
			{
				fo = fo.getParent();
			}
			catch (FileSystemException e1)
			{
				e1.printStackTrace();
				break;
			}
		}
		
		JViewport vp = sp.getViewport();
		Point p = vp.getViewPosition();
		p.x = vp.getWidth();
		vp.setViewPosition( p );
		
		pane.doLayout();
		pane.revalidate();
		pane.repaint();
	}
	
	public FileObject getSelectedPath()
	{
		return selected;
	}
	
	public void shine()
	{
		setBackground( SystemColor.white );
		pane.setBackground( SystemColor.white );
	}
	
	public void dim()
	{
		setBackground( SystemColor.control );
		pane.setBackground( SystemColor.control );
	}
}
